/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.controller;

import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.driving.core.base.BaseController;
import com.driving.core.entity.Exercise;
import com.driving.core.service.IExerciseService;
import com.driving.core.util.Constant;
import com.driving.core.util.Constant.Audience;
import com.driving.core.util.MessageType.ExceptionType;
import com.driving.core.util.Response;

/**
 * 习题控制器
 *
 * @author SAGITTAR
 * @since 2017
 */
@RestController
@RequestMapping(value = "/exercise")
public class ExerciseController extends BaseController<IExerciseService, Exercise> {

    @GetMapping(value = "/chapter/list/{subjectId}/{type}")
    public Response<Map<String, Object>> chapterList(@PathVariable int subjectId, @PathVariable String type) {
        return Response.success(baseService.selectDistinctChapterBySubjectId(subjectId, type));
    }

    /**
     * 获取随机试题，或下一个试题
     *
     * @param request request
     * @param subjectId subjectId
     * @param type type
     * @param chapter chapter
     * @param category category
     * @param id 若大于0，id为当前试题的编号，取相应类别的下一试题；若小于0，则返回随机试题
     * @return 获取随机试题，或下一个试题
     */
    @GetMapping(value = "/{subjectId}/{type}/{chapter}/{category}/{id}")
    public Response<Map<String, Object>> getRandomOrNextOne(HttpServletRequest request, @PathVariable int subjectId,
            @PathVariable String type, @PathVariable int chapter, @PathVariable int category, @PathVariable int id) {
        Integer studentId = getCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        if (Objects.isNull(studentId)) {
            studentId = -1;
        }
        if (id == 0) {
            id = 1;
        }
        return Response.success(baseService.selectRandomOrNextOne(subjectId, type, chapter, category, studentId, id));
    }

    /**
     * 错题与收藏，参数列表，exerciseId为题号；type为类型，1为错题，2为收藏
     *
     * <pre>
     * {
     *     "exerciseId": 1,
     *     "type": 1
     * }
     * </pre>
     *
     * @param request request
     * @param params JSON
     * @return 错题与收藏
     */
    @PostMapping(value = "/collect")
    public Response<Integer> collect(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        int studentId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        if (!params.containsKey("exerciseId") || !params.containsKey("type")) {
            return Response.failure(configReader.getExceptionMessage(ExceptionType.NOT_COMPLETE));
        }
        int exerciseId = Integer.parseInt(params.get("exerciseId").toString());
        int from = Integer.parseInt(params.get("type").toString());
        return Response.success(baseService.collect(studentId, exerciseId, from));
    }

    /**
     * 取消收藏
     *
     * @param request request
     * @param incorrectOrCollectionId incorrectOrCollectionId
     * @return 取消收藏
     */
    @GetMapping(value = "/collect-cancel/{incorrectOrCollectionId}")
    public Response<Boolean> cancelCollect(HttpServletRequest request, @PathVariable int incorrectOrCollectionId) {
        int studentId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        return Response.success(baseService.cancelCollect(studentId, incorrectOrCollectionId));
    }

    /**
     * 查询错题或收藏的题目
     *
     * @param request request
     * @param subjectId 科目，科目一或科目四
     * @param incorrectCollect 1为错题，2为收藏
     * @param id 当前序号，从1开始。如果是-1则随机给题
     * @return 查询错题或收藏的题目
     */
    @GetMapping(value = "/incorrect-collection/{subjectId}/{incorrectCollect}/{id}")
    public Response<Map<String, Object>> incorrectOrCollectedExercise(HttpServletRequest request,
            @PathVariable int subjectId, @PathVariable int incorrectCollect, @PathVariable int id) {
        int studentId = getNonNullCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        if (id == 0) {
            id = 1;
        }
        return Response.success(baseService.selectIncorrectOrCollectedOne(studentId, subjectId, incorrectCollect, id));
    }

    /**
     * 获取模拟考试的试题信息
     *
     * @param subjectId 科目
     * @param type 车型
     * @return 获取模拟考试的试题信息
     */
    @GetMapping(value = "/simulation/list/{subjectId}/{type}")
    public Response<Map<String, Object>> getSimulationList(@PathVariable int subjectId, @PathVariable String type) {
        return Response.success(baseService.selectSimulationBySubjectIdAndType(subjectId, type));
    }

    /**
     * 获取所选试卷的模拟考试题
     *
     * @param examId 试卷编号
     * @param id 试题编号，首次加载为"-1"
     * @return 获取所选试卷的模拟考试题
     */
    @GetMapping(value = "/simulation/detail/{examId}/{id}")
    public Response<Map<String, Object>> getSimulationByExamIdAndId(@PathVariable int examId, @PathVariable int id) {
        return Response.success(baseService.selectSimulationOneByExamIdAndId(examId, id));
    }

    /**
     * 查询图片试题信息分类
     *
     * @param subjectId 科目，目前没用
     * @param type 车型，目前没用
     * @return 查询图片试题信息分类
     */
    @GetMapping(value = "/picture/list/{subjectId}/{type}")
    public Response<Map<String, Object>> getPictureList(@PathVariable int subjectId, @PathVariable String type) {
        return Response.success(baseService.selectListByPictureType(subjectId, type));
    }

    /**
     * 获取该分类下的所有试题
     *
     * @param pictureType pictureType
     * @return 获取该分类下的所有试题
     */
    @GetMapping(value = "/picture/all/{pictureType}")
    public Response<Map<String, Object>> getPictureAll(@PathVariable int pictureType) {
        return Response.success(baseService.selectAllByPictureType(pictureType));
    }

    /**
     * 根据试题编号直接查询
     *
     * @param request request
     * @param id id
     * @return 根据试题编号直接查询
     */
    @GetMapping(value = "/id/{id}")
    public Response<Map<String, Object>> getById(HttpServletRequest request, @PathVariable int id) {
        Integer studentId = getCorrespondingIdByRequest(request.getHeader(Constant.TOKEN), Audience.STUDENT);
        if (Objects.isNull(studentId)) {
            studentId = -1;
        }
        return Response.success(baseService.selectOneById(studentId, id));
    }
}
